<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Section 11.1.&nbsp; String Functions</title>
<link rel="STYLESHEET" type="text/css" href="images/style.css">
<link rel="STYLESHEET" type="text/css" href="images/docsafari.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<br><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td valign="top"><a name="learnphpmysql-CHP-11-SECT-1"></a>
<h3 id="title-IDASIEFF" class="docSection1Title">11.1. String Functions</h3>
<p class="docText">Because you're working with essentially two languages that both support manipulating strings, you need to learn about string functions<a name="IDX-CHP-11-0503"></a> 
<a name="IDX-CHP-11-0504"></a> 
 in PHP and MySQL. You may find it more appropriate to modify a string either in a query or in PHP, based on the particular situation. You're going to learn about the following string operations:</p>
<ul><li><p class="docList">Formatting strings for display</p></li><li><p class="docList">Calculating the length of a string</p></li><li><p class="docList">Changing a string's case to uppercase or lowercase</p></LI><li><p class="docList">Searching for strings within strings and returning the position of the match</p></LI><LI><p class="docList">How to return just a portion of a string, which is a substring</p></LI></ul>
<p class="docText">We'll start with formatting strings, since that will help you throughout the rest of the topics.</p>
<a name="learnphpmysql-CHP-11-SECT-1.1"></a>
<h4 id="title-IDAZJEFF" class="docSection2Title">11.1.1. Formatting Strings for Display</H4>
<p class="docText">So far, you've been using <tt>echo</tt> and <tt>print</tt> to display strings without any modification. You'll learn about two functions called <tt>printf</tt><a name="IDX-CHP-11-0505"></a> 
<a name="IDX-CHP-11-0506"></a> 
<a name="IDX-CHP-11-0507"></a> 
 and <tt>sprintf</tt>. If you're familiar with other programming languages, such as C, you'll recognize that these functions work the same way as they do elsewhere. Don't worry if you haven't used them beforethey're not too hard to work with. The only difference between the two is that <tt>printf</tt> displays a formatted string to the output like <tt>print</tt> does, while <tt>sprintf</tt> saves the string it builds as another string with a name specified by you.</p>
<a name="learnphpmysql-CHP-11-SECT-1.1.1"></a>
<h5 id="title-IDA5KEFF" class="docSection3Title">11.1.1.1. Using printf</H5>
<a name="IDX-CHP-11-0508"></a> 

<p class="docText">The <tt>printf</tt> function works by taking as its first parameter a special <span class="docEmphasis">formatting string</span>. The formatting string works like a template to describe how to plug in the rest of the parameters into one resulting string. You can specify details such as how to format numbers in the string or the padding of values. Each parameter that's placed into the resulting string has a placeholder in the formatting string. For example, to output a binary number, you use the code in <a class="docLink" href="#learnphpmysql-CHP-11-EX-1">Example 11-1</a>.</p>
<a name="learnphpmysql-CHP-11-EX-1"></a><h5 id="title-IDAVLEFF" class="docExampleTitle">Example 11-1. Displaying a number in binary format</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("The computer stores the number 42 internally as %b.",42);
?&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText">This code then produces the output shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-1">Figure 11-1</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-1"></a><p><center>
<h5 class="docFigureTitle">Figure 11-1. Displaying 42 in binary format</H5>
<img border="0" alt="" width="545" height="129" SRC="images/learnphpmysql_1101.jpg">
</center></p><BR>
<p class="docText">The formatting string in <a class="docLink" href="#learnphpmysql-CHP-11-EX-1">Example 11-1</a> contains a placeholder that specifies where to put the second parameter of 42. It begins with a percent sign (<tt>%</tt>), which is called the <span class="docEmphasis">conversion specification</span>. There can be any number of conversion specifications in the formatting string, but they must each have a corresponding parameter when <tt>printf</tt> is called.</p>
<p class="docText">The character after the percent sign is the type specifier. The <span class="docEmphasis">type specifier</span> defines how the parameter is formatted for display when it's placed in the output string, as demonstrated in <a class="docLink" href="#learnphpmysql-CHP-11-EX-2">Example 11-2</a>.</P>
<a name="learnphpmysql-CHP-11-EX-2"></a><H5 id="title-IDAKNEFF" class="docExampleTitle">Example 11-2. printf puts the numbers into the string</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("The computer stores the numbers 42, and 256 internally as %b and %b.",
42,256);
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">When called from a web browser, the code in <a class="docLink" href="#learnphpmysql-CHP-11-EX-2">Example 11-2</a> displays <a class="docLink" href="#learnphpmysql-CHP-11-FIG-2">Figure 11-2</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-2"></a><p><center>
<h5 class="docFigureTitle">Figure 11-2. Including two numbers in the string</h5>
<img border="0" alt="" width="545" height="129" SRC="images/learnphpmysql_1102.jpg">
</center></p><br>
<p class="docText">So far, the only type specifier we've used is <tt>b</tt> for binary, but there are more. <a class="docLink" href="#learnphpmysql-CHP-11-TABLE-1">Table 11-1</a> lists numeric type specifiers.</p>
<a name="learnphpmysql-CHP-11-TABLE-1"></a><P><table cellspacing="0" FRAME="hsides" RULES="all" cellpadding="4" width="100%"><caption><h5 class="docTableTitle">Table 11-1. Type specifiers for numbers</h5></caption><colgroup span="3"><col><col><col></colgroup><thead><TR><th class="thead" scope="col" align="left"><p class="docText">Specifier</P></th><th class="thead" scope="col" align="left"><p class="docText">Meaning</p></th><th class="thead" scope="col" align="left"><p class="docText">Example (using 42)</P></th></tr></thead><tr><td class="docTableCell" align="left"><p class="docText">D</P></td><td class="docTableCell" align="left"><p class="docText">Display as a decimal number</P></td><td class="docTableCell" align="left"><p class="docText">42</P></td></tr><tr><td class="docTableCell" align="left"><p class="docText">B</P></TD><td class="docTableCell" align="left"><p class="docText">Display as a binary number</p></td><td class="docTableCell" align="left"><p class="docText">101010</P></td></TR><tr><TD class="docTableCell" align="left"><p class="docText">C</P></TD><td class="docTableCell" align="left"><p class="docText">Display as ASCII equivalent</p></td><td class="docTableCell" align="left"><p class="docText">*</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">F</p></td><td class="docTableCell" align="left"><p class="docText">Display as a floating-point number, double precision</p></td><TD class="docTableCell" align="left"><p class="docText">42.000000</p></td></TR><TR><td class="docTableCell" align="left"><p class="docText">O</P></td><td class="docTableCell" align="left"><p class="docText">Display as an octal number, base 8</p></TD><td class="docTableCell" align="left"><p class="docText">52</p></TD></tr><tr><TD class="docTableCell" align="left"><p class="docText">S</p></td><td class="docTableCell" align="left"><p class="docText">Display as a string</p></TD><TD class="docTableCell" align="left"><p class="docText">42</p></td></tr><tr><TD class="docTableCell" align="left"><p class="docText">X</p></TD><td class="docTableCell" align="left"><p class="docText">Display as a lowercase hexadecimal</P></TD><TD class="docTableCell" align="left"><p class="docText">2a</p></td></tr><tr><td class="docTableCell" align="left"><p class="docText">X</p></td><td class="docTableCell" align="left"><p class="docText">Display as an uppercase hexadecimal</p></td><td class="docTableCell" align="left"><p class="docText">2A</p></td></tr></table></P><br>
<p class="docText">The last column of <a class="docLink" href="#learnphpmysql-CHP-11-TABLE-1">Table 11-1</a> was generated with the code in <a class="docLink" href="#learnphpmysql-CHP-11-EX-3">Example 11-3</a>.</p>
<a name="learnphpmysql-CHP-11-EX-3"></a><H5 id="title-IDAMUEFF" class="docExampleTitle">Example 11-3. Displaying the same number in different formats</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
$value=42;
printf("%d&lt;br&gt;",$value);
printf("%b&lt;br&gt;",$value);
printf("%c&lt;br&gt;",$value);
printf("%f&lt;br&gt;",$value);
printf("%o&lt;br&gt;",$value);
printf("%s&lt;br&gt;",$value);
printf("%x&lt;br&gt;",$value);
printf("%X&lt;br&gt;",$value);
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-11-EX-3">Example 11-3</a> gives you this column:</p>
<pre>
42
101010
*
42.000000
52
42
2a
2A
</pre><BR>

<p class="docText">In practice, you might use this to convert from an integer to a hexadecimal number if you're building a string when specifying colors in HTML elements. Since you tend to relate better to the decimal value, you can use decimals and have them automatically formatted correctly for display in a tag such as <tt>color="#2a11cc"</tt>.</p>

<a name="learnphpmysql-CHP-11-SECT-1.1.2"></a>
<h5 id="title-IDAEVEFF" class="docSection3Title">11.1.1.2. Padding</H5>
<a name="IDX-CHP-11-0509"></a> 
<a name="IDX-CHP-11-0510"></a> 
<a name="IDX-CHP-11-0511"></a> 

<p class="docText">You can also specify padding for each field. To left pad a field with zeros, place a zero after the conversion specification percent sign (%) followed by the number of zeros to pad the type specifier, as shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-4">Example 11-4</a>. If the output of the parameter uses fewer spaces than the number you specify, zeros are filled in on the left.</p>
<a name="learnphpmysql-CHP-11-EX-4"></a><h5 id="title-IDABWEFF" class="docExampleTitle">Example 11-4. Using left zero padding</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><TD>

<pre>
&lt;?php
printf("Zero padding can help alignment <b>%05d.</b>", 42);
?&gt;
</pre><br>

</td></tr></table></P>
<p class="docText">Padding with zeros gives you the result shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-3">Figure 11-3</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-3"></a><P><center>
<h5 class="docFigureTitle">Figure 11-3. Zero padding to five spaces</H5>
<img border="0" alt="" width="549" height="167" SRC="images/learnphpmysql_1103.jpg">
</center></P><BR>
<p class="docText">Padding with leading spaces, shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-5">Example 11-5</a>, works the same way, except you specify a space after the percent sign instead of a zero.</p>
<a name="learnphpmysql-CHP-11-EX-5"></a><h5 id="title-IDAJXEFF" class="docExampleTitle">Example 11-5. Using left space padding</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("Space padding can be tricky in HTML % <b>5d.</b>", 42);
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">Using the left space padding displays the screen shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-4">Figure 11-4</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-4"></a><p><center>
<h5 class="docFigureTitle">Figure 11-4. Left padding doesn't show up correctly</H5>
<img border="0" alt="" width="549" height="167" SRC="images/learnphpmysql_1104.jpg">
</center></p><br>
<p class="docText">As you can see in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-4">Figure 11-4</a>, the spacing before 42 was ignored by the web browser. You can fix that by using the HTML <tt>&lt;pre&gt;</tt> tag. The <tt>&lt;pre&gt;</tt> HTML markup is used to enclose preformatted text. In the tag, all spaces and line breaks are rendered literally. Additionally, the <tt>&lt;pre&gt;</tt> text renders in a fixed-pitch font. See <a class="docLink" href="#learnphpmysql-CHP-11-EX-6">Example 11-6</a>.</P>
<a name="learnphpmysql-CHP-11-EX-6"></a><H5 id="title-IDA5YEFF" class="docExampleTitle">Example 11-6. Adding the &lt;pre&gt; and &lt;/pre&gt; tags so the spaces display</h5><P><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("&lt;<b>pre</b>&gt;Space padding can be tricky in HTML % 5d.&lt;<B>/pre</b>&gt;", 42);
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-11-FIG-5">Figure 11-5</a>, we correctly see the spaces.</p>
<a name="learnphpmysql-CHP-11-FIG-5"></a><P><center>
<h5 class="docFigureTitle">Figure 11-5. The spaces show up now</h5>
<img border="0" alt="" width="549" height="167" SRC="images/learnphpmysql_1105.jpg">
</center></p><br>
<p class="docText">If you don't specify the character to pad, as happens in <a class="docLink" href="#learnphpmysql-CHP-11-EX-7">Example 11-7</a>, <tt>printf</tt> assumes space padding and outputs a formatted string, like our example in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-5">Figure 11-5</a>.</P>
<a name="learnphpmysql-CHP-11-EX-7"></a><H5 id="title-IDAQ0EFF" class="docExampleTitle">Example 11-7. Left padding using the default of spaces</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("&lt;pre&gt;Space padding can be tricky in HTML %<B>5d.</b>&lt;/pre&gt;", 42);
?&gt;
</pre><br>

</TD></tr></table></P>
<p class="docText">This code is equivalent to <a class="docLink" href="#learnphpmysql-CHP-11-EX-5">Example 11-5</a>, and produces the same result, shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-6">Figure 11-6</a>.</P>
<a name="learnphpmysql-CHP-11-FIG-6"></a><P><center>
<h5 class="docFigureTitle">Figure 11-6. Still left padded</h5>
<img border="0" alt="" width="549" height="167" SRC="images/learnphpmysql_1106.jpg">
</center></p><br>
<p class="docText">To right pad fields, simply put a negative number in the padding field, as in <a class="docLink" href="#learnphpmysql-CHP-11-EX-8">Example 11-8</a>.</p>
<a name="learnphpmysql-CHP-11-EX-8"></a><h5 id="title-IDA31EFF" class="docExampleTitle">Example 11-8. Right padding with spaces</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("<b>&lt;pre&gt;</b>Space padding can be tricky in HTML %-5d.<b>&lt;/pre&gt;</b>", 42);
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText">And the output from the negative number in the padding field displays <a class="docLink" href="#learnphpmysql-CHP-11-FIG-7">Figure 11-7</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-7"></a><P><center>
<H5 class="docFigureTitle">Figure 11-7. Padding on the right</h5>
<img border="0" alt="" width="549" height="165" SRC="images/learnphpmysql_1107.jpg">
</center></P><br>

<a name="learnphpmysql-CHP-11-SECT-1.1.3"></a>
<h5 id="title-IDAA3EFF" class="docSection3Title">11.1.1.3. Specifying precision</h5>
<a name="IDX-CHP-11-0512"></a> 
<a name="IDX-CHP-11-0513"></a> 
<a name="IDX-CHP-11-0514"></a> 

<p class="docText">Sometimes you'll want to change how many digits appear after a decimal point for a real (floating-point) number. This is especially true if you need to print in currency format. To specify the number of digits to use after the decimal point, use a conversion specifier that has a decimal point after the percentage sign followed by the number of decimals. For example, the following code shows you how to do it:</P>
<pre>
%.<tt><i>number_of_decimals_to_display</i></tt>f
</pre><BR>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-11-EX-9">Example 11-9</a> shows a value of 42.4242 set to display as currency.</p>
<a name="learnphpmysql-CHP-11-EX-9"></a><h5 id="title-IDAD4EFF" class="docExampleTitle">Example 11-9. Displaying a real number in money format</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
printf("Please pay $%<b>.2f.</b> ", 42.4242);
?&gt;
</pre><BR>

</TD></tr></table></p>
<p class="docText">Our code displays with the dollar sign and decimal correctly, as shown in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-8">Figure 11-8</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-8"></a><p><center>
<H5 class="docFigureTitle">Figure 11-8. Only two decimal points display</h5>
<img border="0" alt="" width="549" height="165" SRC="images/learnphpmysql_1108.jpg">
</center></P><br>
<p class="docText">Even if you replace the value of 42.4242 with 42, <a class="docLink" href="#learnphpmysql-CHP-11-EX-9">Example 11-9</a> would still print two zeros after the decimal point, since you told <tt>printf</tt> that you always want to print two digits after the decimal point.</P>
<pre>
Please pay $42.00.
</pre><BR>

<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-11-FIG-9">Figure 11-9</a> breaks apart the conversion specifier <tt>%08.2f</tt>.</P>
<a name="learnphpmysql-CHP-11-FIG-9"></a><p><center>
<h5 class="docFigureTitle">Figure 11-9. The segments of a conversion specifier</h5>
<img border="0" alt="" width="366" height="221" SRC="images/learnphpmysql_1109.jpg">
</center></p><br>
<p class="docText">The conversion specification in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-9">Figure 11-9</a> means that you'll print the floating-point number left padded with zeros to eight total spaces. There'll be two digits after the decimal place.</p>

<a name="learnphpmysql-CHP-11-SECT-1.1.4"></a>
<h5 id="title-IDAQAFFF" class="docSection3Title">11.1.1.4. Using sprintf</h5>
<a name="IDX-CHP-11-0515"></a> 
<a name="IDX-CHP-11-0516"></a> 
<a name="IDX-CHP-11-0517"></a> 
<a name="IDX-CHP-11-0518"></a> 
<a name="IDX-CHP-11-0519"></a> 

<p class="docText">The <tt>sprintf</tt> function works exactly the same way as <tt>print</tt>, except its output is sent to a string.</p>
<p class="docText">In <a class="docLink" href="#learnphpmysql-CHP-11-EX-10">Example 11-10</a>, the output string is assigned to the variable <tt>$total</tt>. From there, it could be used in further processing or, in this case, printed to the screen using <tt>echo</tt>.</p>
<a name="learnphpmysql-CHP-11-EX-10"></a><h5 id="title-IDAACFFF" class="docExampleTitle">Example 11-10. Using sprintf with a variable</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><TD>

<pre>
&lt;?php
$total=<b>sprintf</b>("Please pay $%.2f. ", 42.4242);
echo $total;
?&gt;
</pre><br>

</TD></TR></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-11-FIG-10">Figure 11-10</a> displays the result.</P>
<a name="learnphpmysql-CHP-11-FIG-10"></a><p><center>
<h5 class="docFigureTitle">Figure 11-10. The output of the $total variable</h5>
<img border="0" alt="" width="549" height="165" SRC="images/learnphpmysql_1110.jpg">
</center></P><br>
<p class="docText">Sometimes you'll be working with strings that come from external sources, so you'll need to find out information about them. This information might include whether they contain certain strings or may simply be their length.<a name="IDX-CHP-11-0520"></a> 
 Remember that strings are more or less ordered lists of characters. Think of specific characters in a string as an exact numeric location of the string.</p>


<a name="learnphpmysql-CHP-11-SECT-1.2"></a>
<H4 id="title-IDAJDFFF" class="docSection2Title">11.1.2. Length</h4>
<p class="docText">The PHP function <tt>strlen</tt><a name="IDX-CHP-11-0521"></a> 
<a name="IDX-CHP-11-0522"></a> 
 can be used to find out how many characters are in a string. This is very useful for validating that there's data in a string, and that a string isn't larger than it should be. <a class="docLink" href="#learnphpmysql-CHP-11-EX-11">Example 11-11</a> shows how to use this.</p>
<a name="learnphpmysql-CHP-11-EX-11"></a><H5 id="title-IDADEFFF" class="docExampleTitle">Example 11-11. Calculating the length of a string</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
  $password="secret1";

  if (strlen($password) &lt;= 5)
  {
    echo("Passwords must be a least 5 characters long.");
  }
  else {
    echo ("Password accepted.");
  }
?&gt;
</pre><BR>

</TD></tr></table></p>
<p class="docText">Our password code above displays the screen in <a class="docLink" href="#learnphpmysql-CHP-11-FIG-11">Figure 11-11</a>.</p>
<a name="learnphpmysql-CHP-11-FIG-11"></a><p><center>
<H5 class="docFigureTitle">Figure 11-11. The password wasn't long enough to be secure</h5>
<img border="0" alt="" width="519" height="152" SRC="images/learnphpmysql_1111.jpg">
</center></P><br>
<p class="docText">We're going to discuss changing the case of a string next. If you recall, this was an important bullet point when we started talking about string functions.</P>

<a name="learnphpmysql-CHP-11-SECT-1.3"></a>
<H4 id="title-IDADFFFF" class="docSection2Title">11.1.3. Changing Case</H4>
<a name="IDX-CHP-11-0523"></a> 
<a name="IDX-CHP-11-0524"></a> 

<p class="docText">PHP provides functionality for changing the case of a string to all uppercase, all lowercase, or the first letter of a word to uppercase. The commands are <tt>strtoupper,<a name="IDX-CHP-11-0525"></a> 
<a name="IDX-CHP-11-0526"></a> 
<a name="IDX-CHP-11-0527"></a> 
 strtolower</tt>,<a name="IDX-CHP-11-0528"></a> 
<a name="IDX-CHP-11-0529"></a> 
<a name="IDX-CHP-11-0530"></a> 
 and <tt>ucwords</tt>, respectively. <a class="docLink" href="#learnphpmysql-CHP-11-EX-12">Example 11-12</a> uses each of them with the same string.</p>
<a name="learnphpmysql-CHP-11-EX-12"></a><h5 id="title-IDA4GFFF" class="docExampleTitle">Example 11-12. Using the word case functions</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><tr><td>

<pre>
&lt;?php
  $username="John Doe";
  echo("$username in upper case is ".strtoupper($username).".&lt;br&gt;");
  echo("$username in lower case is ".strtolower($username).".&lt;br&gt;");
  echo("$username in first letter upper case is ".ucwords($username).".&lt;br&gt;");
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">The code in <a class="docLink" href="#learnphpmysql-CHP-11-EX-12">Example 11-12</a> displays lowercase, uppercase, and other details.</p>
<pre>
John Doe in upper case is JOHN DOE.
John Doe in lower case is john doe.
John Doe in first letter upper case is John Doe.
</pre><br>

<p class="docText">Number and other symbols are not affected.</p>
<p class="docText">Using <tt>strtoupper</tt> returns strings with all alphabetic characters converted to uppercase. Whereas <tt>strtolower</tt> returns a string with all alphabetic characters converted to lowercase. There's a caveat to this functionality, however: any characters with accents (circumflex, grave, tilde, umlaut, and all other accents on letters) won't be converted to lowercase. <tt>ucwords</tt> returns a string with the first character of each word capitalized, assuming that character is alphabetic. There's one more that we didn't show you in our code, but would be helpful to have in your back pocket: <tt>ucfirst</tt>, which performs the same as <tt>ucwords</tt> by making the first letter of every word an uppercase letter.</p>

<a name="learnphpmysql-CHP-11-SECT-1.4"></a>
<H4 id="title-IDAEIFFF" class="docSection2Title">11.1.4. Checking for a String</h4>
<a name="IDX-CHP-11-0531"></a> 
<a name="IDX-CHP-11-0532"></a> 
<a name="IDX-CHP-11-0533"></a> 
<a name="IDX-CHP-11-0534"></a> 
<a name="IDX-CHP-11-0535"></a> 

<p class="docText">To detect whether a string is part of another string, use <tt>strstr</tt>,<a name="IDX-CHP-11-0536"></a> 
 shown in <a class="docLink" href="#learnphpmysql-CHP-11-EX-13">Example 11-13</a>. This function takes two parameters: the string to search through and the string to search for. It is not case sensitive; if you want to use a function that is case sensitive, use <tt>stristr</tt>. Lastly, there is <tt>strops</tt>, which finds the position of every first occurrence of the string you specified.</p>
<a name="learnphpmysql-CHP-11-EX-13"></a><H5 id="title-IDAXJFFF" class="docExampleTitle">Example 11-13. Detecting whether a string is contained in another string</H5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><td>

<pre>
&lt;?php
  $password="secretpassword1";

  if (strstr($password,"password")){
    echo('Passwords cannot contain the word "password".');
  }
  else {
    echo ("Password accepted.");
  }
?&gt;
</pre><br>

</td></TR></table></p>
<p class="docText"><a class="docLink" href="#learnphpmysql-CHP-11-EX-13">Example 11-13</a> outputs the following:</p>
<pre>
Passwords cannot contain the word "password".
</pre><BR>

<p class="docText">Sometimes it's useful to also know the position of a string that matches another string.</p>

<a name="learnphpmysql-CHP-11-SECT-1.5"></a>
<h4 id="title-IDAMKFFF" class="docSection2Title">11.1.5. Using String Position and Substring to Extract a Portion of a String</H4>
<p class="docText">We're going to use several string functions together. Let's take the string <tt>testing testing Username:Michele Davis</tt> and retrieve only the name. <a class="docLink" href="#learnphpmysql-CHP-11-EX-14">Example 11-14</a> shows how several functions can be used together to search and extract a portion of string.</p>
<a name="learnphpmysql-CHP-11-EX-14"></a><h5 id="title-IDA2KFFF" class="docExampleTitle">Example 11-14. Using several functions together to extract a portion of a string</h5><p><table cellspacing="0" width="90%" border="1" cellpadding="5"><TR><TD>

<pre>
&lt;?php
  $test_string="testing testing Username:Michele Davis";
  $position=strpos($test_string,"Username:");

  //add on the length of the Username:
  $start=$position+strlen("Username:");

  echo "$test_string&lt;br&gt;";
  echo "$position&lt;br&gt;";
  echo substr($test_string,$start);
?&gt;
</pre><br>

</td></tr></table></p>
<p class="docText">Use <tt>strpos</tt> to search for <tt>Username:</tt> and return its position in the string with zero being the first position. Use <tt>strlen</tt> to add on to that position to find where you need to start extracting from the <tt>$test_string</tt>. To extract the name, use <tt>substr</tt>, which takes the string as a parameter, returning everything after the <tt>$position</tt> character in the string. <a class="docLink" href="#learnphpmysql-CHP-11-FIG-12">Figure 11-12</a> shows the end result of your labor.</P>
<a name="learnphpmysql-CHP-11-FIG-12"></a><p><center>
<H5 class="docFigureTitle">Figure 11-12. Pulling the username out of a larger string</h5>
<img border="0" alt="" width="545" height="162" SRC="images/learnphpmysql_1112.jpg">
</center></P><BR>
<p class="docText">The number 16 in our example is the position of our username. If you look at the code, it says:</P>
<pre>
  echo "$position&lt;br&gt;";
</pre><br>

<p class="docText">This is where the 16 comes from.</p>
<p class="docText">Next up, we'll introduce how to display and work with dates and times.</p>


<a href="11011536.html"><img src="images/pixel.jpg" alt="" width="1" height="1" border="0"></a></TD></TR></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11.html><img src="images/prev.gif" width="60" height="17" border="0" align="absmiddle" alt="Previous Page"></a>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href=learnphpmysql-CHP-11-SECT-2.html><img src="images/next.gif" width="60" height="17" border="0" align="absmiddle" alt="Next Page"></a>
</div></td></tr></table>
<script type="text/javascript"><!--
google_ad_client = "pub-0203281046321155";
google_alternate_ad_url = "http://www.bookhtml.com/adbrite.htm";
google_ad_width = 728;
google_ad_height = 90;
google_ad_format = "728x90_as";
google_ad_type = "text_image";
google_ad_channel ="4867465545";
google_color_border = "FFFFFF";
google_color_link = "0000FF";
google_color_bg = "FFFFFF";
google_color_text = "000000";
google_color_url = "0000FF";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</html>
